package messageJSON

import (
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"system-admin/internal/models"
)

/*
用户处理消息集
*/

// 201:验证成功可注册
func UserRegisterVeriftyOK(c *gin.Context) (message string) {
	code := "201"
	message = "验证成功，可注册"
	log.Println(code + ": " + message)

	c.JSON(http.StatusOK, &models.MessageModel{
		Code:    code,
		Message: message,
		Data:    "",
	})
	return
}

// 401:用户已存在，注册失败
func UserRegisterExistsFail(c *gin.Context) (message string) {
	code := "401"
	message = "用户名已存在，无法注册"
	log.Println(code + ": " + message)

	c.JSON(http.StatusOK, &models.MessageModel{
		Code:    code,
		Message: message,
		Data:    "",
	})
	return
}

// 202:用户登录校验通过，可登录
func UserLoginVerifyOK(c *gin.Context) (message string) {
	code := "202"
	message = "用户登录校验通过，可登录"
	log.Println(code + ": " + message)

	c.JSON(http.StatusOK, &models.MessageModel{
		Code:    code,
		Message: message,
		Data:    "",
	})
	return
}

// 402:用户登录验证失败，信息不匹配
func UserLoginVerifyFail(c *gin.Context) (message string) {
	code := "402"
	message = "用户登录验证失败，信息不匹配"
	log.Println(code + ": " + message)

	c.JSON(http.StatusOK, &models.MessageModel{
		Code:    code,
		Message: message,
		Data:    "",
	})
	return
}

// 203:获取用户注册排行榜成功
func GetUserRegisterRankingBoardOK(c *gin.Context, userList *models.UserList, row int) (message string) {
	code := "203"
	message = fmt.Sprintf("获取用户注册排行榜成功，返回了%d条数据", row)
	log.Println(code + ": " + message)

	//将结构体转成byte[]再转成string装入请求中
	userListJSON, err := json.Marshal(&userList)
	userListJSONString := string(userListJSON)

	if err != nil {
		log.Println(err)
	}

	c.JSON(http.StatusOK, &models.MessageModel{
		Code:    code,
		Message: message,
		Data:    userListJSONString,
	})
	return
}

// 403:获取用户注册排行榜失败
func UserRegisterRankingBoardFail(c *gin.Context) (message string) {
	code := "401"
	message = "获取用户注册排行榜失败"
	log.Println(code + ": " + message)

	c.JSON(http.StatusOK, &models.MessageModel{
		Code:    code,
		Message: message,
		Data:    "",
	})
	return
}
